package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.depot.AreaBean;
import com.scisoft.scan.data.depot.DepotOrderStatis;
import com.scisoft.scan.data.depot.DepotQueryBean;
import com.scisoft.scan.data.stockdistribute.StockAreaBean;
import com.scisoft.scan.data.stockdistribute.StockAreaPositionBean;
import com.scisoft.scan.data.stockdistribute.StockDistributeBean;
import com.scisoft.scan.data.stockdistribute.StockPositionBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_StockDistribute {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/StockDistributeService?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://stockdistribute.scan.ws.dms.com/";

    //查询库存的分页信息 bizId:模糊搜索的条件
    public static QueryPageBean STOCKDISTRIBUTE_01(String bizId) throws Exception {
//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 2;
//        ret.totalCount = 18;
//        ret.pageSize = 10;
//        return ret;

        String arg0 = String.format( "{\"flowNo\":\"STOCKDISTRIBUTE_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询库存列表 bizId:模糊搜索的条件 pagesize 每页的数量 pageindex 第几页,页码应该从1开始
    public static List<StockDistributeBean> STOCKDISTRIBUTE_02(String bizId, int pagesize, int pageindex) throws Exception {

        List<StockDistributeBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCKDISTRIBUTE_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockDistributeBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //上传配件的库区信息
    public static String STOCKDISTRIBUTE_03(StockDistributeBean stockInfoPictureBean) throws Exception {

//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(stockInfoPictureBean);
        String arg0 = String.format("{\"flowNo\":\"STOCKDISTRIBUTE_03\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }


    //根据仓库ID查询仓库所有的库区
    public static List<StockAreaBean> STOCKDISTRIBUTE_04(String depotId)throws Exception {
//        List<StockAreaBean> ret = new ArrayList<>();
//        StockAreaBean s1 = new StockAreaBean();
//        s1.areaId = "J1";
//        s1.areaName = "J区";
//        ret.add(s1);
//        StockAreaBean s2 = new StockAreaBean();
//        s2.areaId = "B2";
//        s2.areaName = "B区";
//        ret.add(s2);
//        return ret;
        String arg0 = String.format("{\"flowNo\":\"STOCKDISTRIBUTE_04\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, depotId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<StockAreaBean> gret = new ArrayList<StockAreaBean>();
        List<StockAreaBean> ret = new ArrayList<StockAreaBean>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<StockAreaBean>>() {
            }.getType());
            for (int i = 0; i < gret.size(); i++) {
                StockAreaBean tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //根据仓库ID、库区ID查询所有的货架
    public static List<StockPositionBean> STOCKDISTRIBUTE_05(String depotId,String areaId)throws Exception {
//        List<StockPositionBean> ret = new ArrayList<>();
//        StockPositionBean s1 = new StockPositionBean();
//        s1.positionId = "1";
//        s1.positionName = "B-A11" + areaId;
//        ret.add(s1);
//        StockPositionBean s2 = new StockPositionBean();
//        s2.positionId = "2";
//        s2.positionName = "B-A32" + areaId;
//        ret.add(s2);
//        return ret;
        String bizId = depotId+","+areaId;
        String arg0 = String.format("{\"flowNo\":\"STOCKDISTRIBUTE_05\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<StockPositionBean> gret = new ArrayList<StockPositionBean>();
        List<StockPositionBean> ret = new ArrayList<StockPositionBean>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<StockPositionBean>>() {
            }.getType());
            for (int i = 0; i < gret.size(); i++) {
                StockPositionBean tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //根据货架反向查询库区
    public static StockAreaPositionBean STOCKDISTRIBUTE_06(String depotId,String positionName)throws Exception {
//        String bizId = depotId + ","+ positionName;
//        StockAreaPositionBean ret = new StockAreaPositionBean();
//        ret.areaId = "1";
//        ret.areaName = "库区1";
//        ret.positionId = "1";
//        ret.positionName = "货架1";
//        return ret;
        String bizId = depotId +","+positionName;
        String arg0 = String.format( "{\"flowNo\":\"STOCKDISTRIBUTE_06\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        StockAreaPositionBean ret = new StockAreaPositionBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<StockAreaPositionBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }


    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
